//
//  ChooseJoinClubVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/15.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ChooseJoinClubVC.h"
#import "MyJoinClubCell.h"
#import "MyJoinClubDetailVC.h"
@interface ChooseJoinClubVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ChooseJoinClubVC
{
    UITableView *_tableView;
    NSInteger _currentPage;
    NSMutableArray *_selIndexArr;
    
    //请求下来的字典
    NSDictionary *_responseDic;
    //请求下来的数组
    NSMutableArray *_responseArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"选择加入的俱乐部";
    _responseArr = [NSMutableArray array];
    _currentPage = 1;
    [self requestForWillJoinClubListCurrentPage:_currentPage isFirst:YES];
    [self initTableViewAndBtn];
    _selIndexArr = [[NSMutableArray alloc]init];
    self.navigationItem.backBarButtonItem= [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    //设置多余的分割线不显示
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
#pragma - mrak RequestForWillJionClubList
- (void)requestForWillJoinClubListCurrentPage:(NSInteger)currentPage isFirst:(BOOL)isFirst{
    //参数
    NSDictionary *parameters = @{
                                 @"userID": [NSString readToNSDefaultsWithKey:kId],
                                 @"status":@"0",
//                                 @"isFeatured":@"0",
                                 @"pageNum": @(_currentPage),
                                 @"pageSize":@"10",
                                 };
    [SVProgressHUD showWithStatus:@"请求中"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/club/clubListByUserID.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        [_tableView.mj_header endRefreshing];
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        NSArray *temp = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
            
        }else{
            if (isFirst) {
                if (temp.count == 0) {
                    [SVProgressHUD showImage:nil status:@"暂无数据"];
                }
                [_responseArr removeAllObjects];
            }
            [_responseArr addObjectsFromArray:temp];
            
            if (temp.count < 10) {
                [_tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                [_tableView.mj_footer endRefreshing];
            }
            //请求成功
            [_tableView reloadData];
            [SVProgressHUD dismiss];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}
- (void)initTableViewAndBtn{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self refreshDown];
    [self refreshLoadMore];
}
//下拉刷新
- (void)refreshDown{
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _currentPage = 1;
        [self requestForWillJoinClubListCurrentPage:_currentPage isFirst:YES];
    }];
}
//上提加载更多
- (void)refreshLoadMore{
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _currentPage++;
        [self requestForWillJoinClubListCurrentPage:_currentPage isFirst:NO];
    }];
}
#pragma - mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyJoinClubCell *cell = [[NSBundle mainBundle]loadNibNamed:@"MyJoinClubCell" owner:nil options:nil][0];
    cell.tintColor = Theme_Color;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.clubName.text = _responseArr[indexPath.row][@"clubName"];
    cell.clubCity.text = _responseArr[indexPath.row][@"city"];
    NSNumber *nub = _responseArr[indexPath.row][@"peopleNum"];
    cell.clubMemberNum.text = [NSString stringWithFormat:@"%@%@",nub.stringValue,@"人"];
    NSString *urlStr = _responseArr[indexPath.row][@"logo"];
    //定向拼接替换logo的id标识
    NSString *latreStr = [urlStr substringFromIndex:47];//.png或者.jpg
    urlStr = [urlStr substringToIndex:40];
    NSString *userId = _responseArr[indexPath.row][@"userID"];
    urlStr = [NSString stringWithFormat:@"%@%@%@%@",urlStr, @"user/",userId,latreStr];
    
    //获取当前时间
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYYMMddHHmmssSSS"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    urlStr = [NSString stringWithFormat:@"%@%@%@",urlStr, @"?",dateString];
    [cell.clubIcon sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"userIcon"]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyJoinClubDetailVC *vc = [[MyJoinClubDetailVC alloc]init];
    vc.clubId = _responseArr[indexPath.row][@"id"];
    vc.clubName = _responseArr[indexPath.row][@"clubName"];
    vc.clubSummary = _responseArr[indexPath.row][@"clubSummary"];
    NSString *urlStr = _responseArr[indexPath.row][@"logo"];
    //定向拼接替换logo的id标识
    NSString *latreStr = [urlStr substringFromIndex:47];//.png或者.jpg
    urlStr = [urlStr substringToIndex:40];
    NSString *userId = _responseArr[indexPath.row][@"userID"];
    urlStr = [NSString stringWithFormat:@"%@%@%@%@",urlStr, @"user/",userId,latreStr];
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYYMMddHHmmssSSS"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    vc.clubIconStr = [NSString stringWithFormat:@"%@%@%@",urlStr, @"?",dateString];
    [self.navigationController pushViewController:vc animated:YES];

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
